import { join } from 'path';
import { config } from 'dotenv';
const path = require('path');

const scripts =
  process.env.KLM_ENV === 'prod'
    ? [
        {
          innerHTML: '',
          body: true,
        },
      ]
    : [];


const plugins = ['@/plugins/vant', '@/plugins/http']
if(process.env.KLM_ENV === 'prod'){
  plugins.push({ src: '@/plugins/debug', ssr: false })
}
export default {
  // Global page headers: https://go.nuxtjs.dev/config-head
  head: {
    title: '',
    htmlAttrs: {
      lang: 'en',
    },
    meta: [
      { charset: 'utf-8' },
      {
        name: 'viewport',
        content: 'width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0',
      },
      { hid: 'description', name: 'description', content: '' },
      { name: 'format-detection', content: 'telephone=no' },
    ],
    link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }],
    script: scripts,
  },

  // Global CSS: https://go.nuxtjs.dev/config-css
  css: [
    '@/assets/style/global.css',
    '@/assets/style/reset.css',
    '@/assets/style/border-retain.css',
  ],

  // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
  plugins,

  // Auto import components: https://go.nuxtjs.dev/config-components
  components: true,

  // Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
  buildModules: [],

  // Modules: https://go.nuxtjs.dev/config-modules
  modules: ['@nuxtjs/axios', ['cookie-universal-nuxt', { alias: 'cookiz' }]],

  // Build Configuration: https://go.nuxtjs.dev/config-build
  build: {
    // 添加这个是关键，添加后babel才会处理依赖包vant里面的代码
    transpile: [/vant.*?less/],
    babel: {
      plugins: [
        [
          'import',
          {
            libraryName: 'vant',
            style: (name) => `${name}/style/less.js`,
          },
          'vant',
        ],
      ],
    },
    extend(_config) {
      _config.module.rules.push({
        test: /\.less$/,
        use: [
          {
            loader: 'less-loader',
            options: {
              javascriptEnabled: true,
              modifyVars: {
                hack: `true; @import "${path.join(__dirname, './assets/style/vantVar.less')}";`,
              },
            },
          },
        ],
      });
    },
  },
  server: {
    port: 8870,
    host: '0.0.0.0',
  },
  env: {
    KLM_ENV: process.env.KLM_ENV,
    ...config({
      path: join(__dirname, `.env.${process.env.KLM_ENV}`),
    }).parsed,
  },
  router: {
    base: '/klm',
    middleware: 'query',
  },
};
